import vue from 'vue';
import VueRouter from 'vue-router';

import Login from '@/components/Login.vue';
import Main from '@/components/Main.vue';
import Welcome from '@/components/Welcome.vue';
import Users from '@/components/user/Users.vue';

import unmakeList from '@/components/storageCell/unmakeList.vue';
import query from '@/components/storageCell/query.vue';
import list from '@/components/storageCell/list.vue';

vue.use(VueRouter);

//创建
const router=new VueRouter({
	routes: [
	    //设置重定向
	    { path: '/', redirect: '/login' },
	    //登录组件
	    { path: '/login', component: Login },
	    //后台主页组
		{
		    path: '/main', 
		    component: Main,
		    redirect: '/welcome',
		    children: [
		        { path: '/welcome', component: Welcome },
		        { path: '/users', component: Users },
				{ path: '/unmakeList', component: unmakeList },
				{ path: '/query', component: query },
				{ path: '/list', component: list },
		    ]
		},
	]
})

//全局导航守卫
router.beforeEach((to, from, next)=>{
    //如果访问的路由是/login, 不判断直接放行
    if(to.path === '/login')
        return next()
    //从sessionStorage中获取token
    const token = window.sessionStorage.getItem('token')
    if(!token)
        return next('/login')
    next()
})

//共享
export default router